<?php
class RolesController extends Controller
{
	public $layout = 'main';

	public function actionIndex()
	{
		//get params
		$intPage 		= Yii::app()->request->getParam('page', 1);
		$intRowsPerPage = Yii::app()->request->getParam('rpp', MIN_ROWS_PER_PAGE);
		$intSortType 	= Yii::app()->request->getParam('sort',-1);
		
		//list roles
		$arrRoles = Roles::getInstance()->getList(array(), $intPage, $intRowsPerPage);
		$intTotalRows = isset($arrUsers[0]['total_rows'])?$arrUsers[0]['total_rows']:0;
		
		//pagination
		$objPages=new CPagination($intTotalRows);
		$objPages->setCurrentPage($intPage-1);
		$objPages->setPageSize($intRowsPerPage);
		
		//render view
		$this->render('index', array(
			'arrRoles' => $arrRoles,
			'objPages' => $objPages
		));
	}
	
	public function actionForm()
	{
		$intUserId = Yii::app()->request->getParam('id', null);
		//update
		if(Yii::app()->request->isPostRequest)
		{
			$arrParams = Yii::app()->request->getParam('user', null);
			if(empty($intPostId)){
				$intResult = Users::getInstance()->insert($arrParams);
				$intPostId = $intResult;
			}
			else{
				$intResult = Users::getInstance()->update($intUserId, $arrParams);
			}
		}
		//load form
		$arrItemInfo = array();
		if(!empty($intUserId)){
			$arrItemInfo = Users::getInstance()->get($intUserId);
		}
		$arrRoles = Roles::getInstance()->getList();
		//render layout
		$this->render('form', array(
			'arrItemInfo' => $arrItemInfo,
			'arrRoles' => $arrRoles
		));
	}
	
	public function actionDelete(){
		$intId = Yii::app()->request->getParam('id', null);
		if(!empty($intId))
			$result = Users::getInstance()->delete($intId);
		header('Location: '.LINK_ADMINCP_USERS);
	}

}